package main

import (
	"fmt"
	"gopl/ch7/stringslice"
	"gopl/ch7/track"
	"html/template"
	"log"
	"net/http"
	"os"
	"sort"
	"text/tabwriter"
	"time"
)

var tracks = []*track.Track{
	{"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
	{"Go", "Moby", "Moby", 1992, length("3m37s")},
	{"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
	{"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}

func length(s string) time.Duration {
	d, err := time.ParseDuration(s)
	if err != nil {
		panic(s)
	}
	return d
}

func printTracks(tracks []*track.Track) {
	const format = "%v\t%v\t%v\t%v\t%v\t\n"
	tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
	fmt.Fprintf(tw, format, "Title", "Artist", "Album", "Year", "Length")
	fmt.Fprintf(tw, format, "------", "------", "------", "------", "------")
	for _, track := range tracks {
		fmt.Fprintf(tw, format, track.Title, track.Artist, track.Album, track.Year, track.Length)
	}
	tw.Flush() // calculate column widths and print table
}

func main() {
	http.HandleFunc("/sorting", sorting)
	log.Fatal(http.ListenAndServe(":8000", nil))
	fields := []string{"Title", "Length"}
	sort.Stable(stringslice.CustomSort1{
		T: tracks,
		Less1: func(x, y *track.Track) bool {
			for _, v := range fields {
				switch v {
				case "Title":
					if x.Title != y.Title {
						return x.Title < y.Title
					}
				case "Artist":
					if x.Artist != y.Artist {
						return x.Artist < y.Artist
					}
				case "Album":
					if x.Album != y.Album {
						return x.Album < y.Album
					}
				case "Year":
					if x.Year != y.Year {
						return x.Year < y.Year
					}
				case "Length":
					if x.Length != y.Length {
						return x.Length < y.Length
					}
				}
			}
			return false
		}, Fields: fields})
	printTracks(tracks)
	values := []int{1, 3, 5, 7, 9, 8, 6, 4, 2}
	fmt.Println(sort.IntsAreSorted(values))
	sort.Ints(values)
	fmt.Println(sort.IntsAreSorted(values))
	sort.Sort(sort.Reverse(sort.IntSlice(values)))
	fmt.Println(values)
	fmt.Println(sort.IntsAreSorted(values))
}

var trackList = template.Must(template.New("trackList").Parse(`
<h1>trackList</h1>
<table>
<tr style="text-align:left">
  <th><a href="http://127.0.0.1:8000/sorting?sort=Title">Title</a></th>
  <th><a href="http://127.0.0.1:8000/sorting?sort=Artist">Artist</a></th>
  <th><a href="http://127.0.0.1:8000/sorting?sort=Album">Album</a></th>
  <th><a href="http://127.0.0.1:8000/sorting?sort=Year">Year</a></th>
  <th><a href="http://127.0.0.1:8000/sorting?sort=Length">Length</a></th>
  </tr>
  {{range .T}}
  <tr>
    <td>{{.Title}}</td>
	<td>{{.Artist}}</td>
	<td>{{.Album}}</td>
	<td>{{.Year}}</td>
	<td>{{.Length}}</td>
	</tr>
	{{end}}
	</table>
`))

func sorting(w http.ResponseWriter, r *http.Request) {
	fields := r.URL.Query()["sort"]
	fmt.Println(fields)
	list := stringslice.CustomSort1{
		T: tracks,
		Less1: func(x, y *track.Track) bool {
			for _, v := range fields {
				switch v {
				case "Title":
					if x.Title != y.Title {
						return x.Title < y.Title
					}
				case "Artist":
					if x.Artist != y.Artist {
						return x.Artist < y.Artist
					}
				case "Album":
					if x.Album != y.Album {
						return x.Album < y.Album
					}
				case "Year":
					if x.Year != y.Year {
						return x.Year < y.Year
					}
				case "Length":
					if x.Length != y.Length {
						return x.Length < y.Length
					}
				}
			}
			return false
		}, Fields: fields}
	sort.Stable(list)
	if err := trackList.Execute(w, list); err != nil {
		log.Fatal(err)
	}
}
